﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MediaClient.Common
{
    public class ConvertHelper
    {
        private unsafe void* FlexApp = null;
        private static ConvertHelper flexEncoder = null;


        [DllImport("FlexConvert.dll", CallingConvention = CallingConvention.Cdecl)]
        private static unsafe extern void* Convert_New();

        public unsafe ConvertHelper()
        {
            try
            {
                FlexApp = Convert_New();
            }
            catch (Exception myE)
            {
                throw myE;
            }
        }

        [DllImport("FlexConvert.dll", CallingConvention = CallingConvention.Cdecl)]
        private static unsafe extern int InitialFlex(string sourcefile, string targetfile);

        public unsafe int InitialFlexConvert(string filename, string targetfile)
        {
            InitialFlex(filename, targetfile);
            return 1;
        }

        [DllImport("FlexConvert.dll", CallingConvention = CallingConvention.Cdecl)]
        private static unsafe extern int Convert_Start();

        public unsafe int StartFlexConvert()
        {            
            Convert_Start();
            return 1;
        }

        [DllImport("FlexConvert.dll")]
        private static unsafe extern int Convert_TotalFrame();

        public unsafe int GetTotalFrame()
        {
            return Convert_TotalFrame();
        }

        [DllImport("FlexConvert.dll")]
        private static unsafe extern int Convert_CurrenFrametCount();

        public unsafe int GetCurrenFrametNum()
        {
            return Convert_CurrenFrametCount();
        }


        public unsafe double GetProcess()
        {
            double process = 0.0;
            int total = Convert_TotalFrame();
            int current = Convert_CurrenFrametCount();
            process = (double)current / (double)total;
            return process;
        }



        //private unsafe void* FlexApp = null;
        //private static ConvertHelper flexEncoder = null;


        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern void* Convert_New();

        //public unsafe ConvertHelper()
        //{
        //    FlexApp = Convert_New();
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern int InitialFlex(string sourcefile,string tFile);

        //public unsafe int InitialFlexConvert(string filename, string tFile)
        //{
        //    InitialFlex(filename, tFile);
        //    return 1;
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern int Convert_Start();

        //public unsafe int StartFlexConvert()
        //{
        //    Convert_Start();
        //    return 0;
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern void Convert_Next();

        //public unsafe void NextFrame()
        //{
        //    Convert_Next();
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern byte* Convert_GetFBuffer();

        //public unsafe byte* GetFirstFiledBuffer()
        //{
        //    return Convert_GetFBuffer();
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern byte* Convert_GetSBuffer();

        //public unsafe byte* GetSecondFiledBuffer()
        //{
        //    return Convert_GetSBuffer();
        //}


        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern void Convert_AddWaterMark(bool flag, byte* buffer);

        //public unsafe void StartAddWaterMark(bool flag, byte* buffer)
        //{
        //    Convert_AddWaterMark(flag, buffer);
        //}

        ////Convert_TotalFrame()
        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern int Convert_TotalFrame();

        //public unsafe int GetTotalFrame()
        //{
        //    return Convert_TotalFrame();
        //}

        //[DllImport("FlexConvert.dll")]
        //private static unsafe extern int Convert_CurrenFrametCount();

        //public unsafe int GetCurrenFrametNum()
        //{
        //    return Convert_CurrenFrametCount();
        //}
    }
}
